/**
 * 底部tab组件
 */
Component({
  data: {
    bgTextStyle: "dark",
    scrollTop: "200rpx",
    bgColor: "#ff0000",
    bgColorTop: "#00ff00",
    bgColorBottom: "#0000ff",
    nbTitle: "标题",
    nbLoading: false,
    nbFrontColor: "#000000",
    nbBackgroundColor: "#ffffff",

    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [
      {
        pagePath: "/pages/base/index/index",
        iconPath: "/assets/image/icon_component.png",
        selectedIconPath: "/assets/image/icon_component_HL.png",
        text: "基础",
      },
      {
        pagePath: "/pages/template/index/index",
        iconPath: "/assets/image/icon_API.png",
        selectedIconPath: "/assets/image/icon_API_HL.png",
        text: "模板",
      },
    ],
  },
  lifetimes: {
    attached() {
      this.init();
    },
  },
  attached() {},
  methods: {
    init() {
      const page = getCurrentPages().pop();
      if (page) {
        let curPageRoute = page.route;
        curPageRoute = curPageRoute.startsWith("/")
          ? curPageRoute
          : `/${curPageRoute}`;
        const idx = this.data.list.findIndex(
          (item) => item.pagePath === curPageRoute
        );
        if (idx >= 0) {
          this.setData({ selected: idx });
        }
      }
    },
    switchTab(e: WechatMiniprogram.CustomEvent) {
      const selectedIndex = e.detail as unknown as number;
      const tabInfo = this.data.list[selectedIndex];
      if (tabInfo) {
        const url = `${tabInfo.pagePath}`;
        wx.switchTab({ url });
      }
      this.setData({ selected: selectedIndex });
    },
  },
});
